﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KongZhiTaiTest
{
    /// <summary>
    /// 索引器测试类
    /// </summary>
    class IndextNames
    {
        /// <summary>
        /// 内部数组
        /// </summary>
        private string[] nameList = new string[10];
        public IndextNames()
        {
            for(int i = 0; i < nameList.Length; i++)
            {
                nameList[i] = "N/A";
            }
        }

        /// <summary>
        /// 创建一个索引器来让我们的class 看起来像是数组。
        /// </summary>
        /// <param name="index">整形作为索引器</param>
        /// <returns></returns>
        public string this[int index] 
        {
            get
            {
                string temp = "";
                if (index > 0 && index < nameList.Length)
                {
                    return temp = nameList[index];
                }
                else
                    return temp;
            }
            set
            {
                if(index >0 && index < nameList.Length)
                {
                    nameList[index] = value;
                }
            }
        }

        /// <summary>
        /// 通过内容找到对应的索引
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int this[string name]
        {
            get
            {
                int index = 0;
                while(index < nameList.Length)
                {
                    if(nameList[index] == name)
                    {
                        return index;
                    }
                    index++;
                }
                return -1;
            }
        }
    }
}
